package com.turing.post.common.impl;

import java.math.BigInteger;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.common.util.SysOpUserRole;
import com.turing.post.common.util.SysOpUserRoleDao;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;

@Stateless(mappedName = "common.impl.SysOpUserRoleDaoImpl")
@Remote
@Local
public class SysOpUserRoleDaoImpl implements SysOpUserRoleDao {

	/**
	 * 实体管理器
	 * 
	 * @see javax.persistence.EntityManager
	 */
	@PersistenceContext
	protected EntityManager _em;

	@Override
	public void deleteSysOpUserRole(BigInteger operId)
			throws PCPSApplicationException {
		List<SysOpUserRole> souList = getSysOpUserRoleByOperId(operId);
		for (int i = 0; i < souList.size(); i++) {
			GeneralDao.remove(_em, souList.get(i));
		}
	}

	@Override
	public SysOpUserRole getSysOpUserRoleByPrimaryKey(
			SysOpUserRole sysOpUserRole) {
		return _em.find(SysOpUserRole.class, sysOpUserRole);
	}

	@Override
	public void insertSysOpUserRole(SysOpUserRole sysOpUserRole)
			throws PCPSApplicationException {
		GeneralDao.insert(_em, sysOpUserRole);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<SysOpUserRole> getSysOpUserRoleByOperId(BigInteger operId) {
		String sql = "select * from SysOpUserRole where operid=" + operId
				+ " order by roleId";
		Query query = _em.createNativeQuery(sql, SysOpUserRole.class);
		return query.getResultList();
	}
}
